// QtCore
#include <QtCore/QFile>
#include <QtCore/QDataStream>
#include <QtCore/QTextStream>
// Локальные
#include "fem/trianglenet.h"

namespace fem
{
    //============================================================================================================================
    //============================================================================================================================
    TriangleNet::TriangleNet(const QString &points_filename, const QString &nvtr_filename, FileFormat format)
    {
        if(format == TriangleNet::NET_T)
        {
            QFile nvtr_file(nvtr_filename);
            QFile points_file(points_filename);
            QTextStream nvtr_file_stream(&nvtr_file);
            QTextStream point_file_stream(&points_file);

            points_file.open(QFile::ReadOnly | QFile::Text);
            while(!point_file_stream.atEnd())
            {
                double x, y;
                point_file_stream >> x >> y;
                m_points.append(geometry::_2d::Point(x, y));
            }
            points_file.close();

            nvtr_file.open(QFile::ReadOnly | QFile::Text);
            while(!nvtr_file_stream.atEnd())
            {
                int material;
                int p1, p2, p3;
                nvtr_file_stream >> p1 >> p2 >> p3 >> material;
                m_nvtr.append(TriangleElement(p1, p2, p3, material));
            }
            nvtr_file.close();

            //Последние строчки в файлах пустые.
            m_nvtr.removeLast();
            m_points.removeLast();
        }
        else if(format == TriangleNet::NET_B)
        {
            QFile nvtr_file(nvtr_filename);
            QFile points_file(points_filename);
            QDataStream nvtr_file_stream(&nvtr_file);
            QDataStream point_file_stream(&points_file);

            nvtr_file_stream.setVersion(QDataStream::Qt_4_7);
            point_file_stream.setVersion(QDataStream::Qt_4_7);

            points_file.open(QFile::ReadOnly);
            while(!point_file_stream.atEnd())
            {
                double x, y;
                point_file_stream >> x >> y;
                m_points.append(geometry::_2d::Point(x, y));
            }
            points_file.close();

            nvtr_file.open(QFile::ReadOnly);
            while(!nvtr_file_stream.atEnd())
            {
                int material;
                int p1, p2, p3;
                nvtr_file_stream >> p1 >> p2 >> p3 >> material;
                m_nvtr.append(TriangleElement(p1, p2, p3, material));
            }
            nvtr_file.close();
        }
    }

    //============================================================================================================================
    //============================================================================================================================
    void TriangleNet::writeToFiles(const QString &points_filename, const QString &nvtr_filename) const
    {
        QFile nvtr_file(nvtr_filename);
        QFile points_file(points_filename);
        QDataStream nvtr_file_stream(&nvtr_file);
        QDataStream points_file_stream(&points_file);

        nvtr_file_stream.setVersion(QDataStream::Qt_4_7);
        points_file_stream.setVersion(QDataStream::Qt_4_7);

        points_file.open(QFile::WriteOnly);
        for(int i = 0; i < m_points.size(); i++)
            points_file_stream << m_points[i].x() << m_points[i].y();
        points_file.close();

        nvtr_file.open(QFile::WriteOnly);
        for(int i = 0; i < m_nvtr.size(); i++)
            nvtr_file_stream << m_nvtr[i].point(0) << m_nvtr[i].point(1) << m_nvtr[i].point(2) << m_nvtr[i].material();
        nvtr_file.close();
    }

    //============================================================================================================================
    //============================================================================================================================
    void TriangleNet::readFromFiles(const QString &points_filename, const QString &nvtr_filename, FileFormat format)
    {
        if(format == TriangleNet::NET_T)
        {
            QFile nvtr_file(nvtr_filename);
            QFile points_file(points_filename);
            QTextStream nvtr_file_stream(&nvtr_file);
            QTextStream point_file_stream(&points_file);

            m_points.clear();
            points_file.open(QFile::ReadOnly | QFile::Text);
            while(!point_file_stream.atEnd())
            {
                double x, y;
                point_file_stream >> x >> y;
                m_points.append(geometry::_2d::Point(x, y));
            }
            points_file.close();

            m_nvtr.clear();
            nvtr_file.open(QFile::ReadOnly | QFile::Text);
            while(!nvtr_file_stream.atEnd())
            {
                int material;
                int p1, p2, p3;
                nvtr_file_stream >> p1 >> p2 >> p3 >> material;
                m_nvtr.append(TriangleElement(p1, p2, p3, material));
            }
            nvtr_file.close();

            //Последние строчки в файлах пустые.
            m_nvtr.removeLast();
            m_points.removeLast();
        }
        else if(format == TriangleNet::NET_B)
        {
            QFile nvtr_file(nvtr_filename);
            QFile points_file(points_filename);
            QDataStream nvtr_file_stream(&nvtr_file);
            QDataStream point_file_stream(&points_file);

            nvtr_file_stream.setVersion(QDataStream::Qt_4_7);
            point_file_stream.setVersion(QDataStream::Qt_4_7);

            m_points.clear();
            points_file.open(QFile::ReadOnly);
            while(!point_file_stream.atEnd())
            {
                double x, y;
                point_file_stream >> x >> y;
                m_points.append(geometry::_2d::Point(x, y));
            }
            points_file.close();

            m_nvtr.clear();
            nvtr_file.open(QFile::ReadOnly);
            while(!nvtr_file_stream.atEnd())
            {
                int material;
                int p1, p2, p3;
                nvtr_file_stream >> p1 >> p2 >> p3 >> material;
                m_nvtr.append(TriangleElement(p1, p2, p3, material));
            }
            nvtr_file.close();
        }
    }
}
